package com.igroupes.ovineherd.config;


import com.igroupes.ovineherd.aop.LoginUserArgumentResolver;
import com.igroupes.ovineherd.aop.LoginUserHandlerInterceptorAdapter;
import com.igroupes.ovineherd.rpc.client.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private UserService userService;


    @Bean
    public LoginUserHandlerInterceptorAdapter loginUserHandlerInterceptorAdapter() {
        return new LoginUserHandlerInterceptorAdapter(userService);
    }


    //添加自定义的拦截器
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        LoginUserArgumentResolver loginUserArgumentResolver = new LoginUserArgumentResolver();
        argumentResolvers.add(loginUserArgumentResolver);
    }

    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginUserHandlerInterceptorAdapter()).addPathPatterns("/**");
    }
}
